package com.video.controller;

import com.video.support.base.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by 执笔 on 2017/3/19.
 */
@Controller
@RequestMapping
public class IndexController extends BaseController {

/*    @Autowired
    private UserService userService;

    @RequestMapping({"/","index"})
    public String index(Model model) {
        Example example = ExampleUtil.getInstance()
                .addOrder("sort", ExampleType.OrderType.DESC)
                .getExample();
        Integer userId = sessionUser() == null ? 0 : sessionUser().getId();
        PageInfo<FoodModel> pageInfo = foodService.selectModel(example, userId, new Page(20));
        model.addAttribute("list", pageInfo.getList());
        return "index";
    }*/

    @RequestMapping({"/","index"})
    public String index(Model model) {
        return "index";
    }

    /**
     * @return
     */
  /*  @RequestMapping(value = "join", method = RequestMethod.GET)
    public String login() {
        if (null != sessionUser()) {
            throw new MessageException("请先退出当前登录用户");
        }
        return "/join";
    }


    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(String phone, String password) {
        User user = userService.selectByPhone(phone);
        if (null == user) {
            throw new MessageException("该用户不存在");
        }
        if (!user.getPassword().equalsIgnoreCase(SecurityUtil.MD5(password, PASSWORD_COUNT))) {
            throw new MessageException("用户名或密码错误");
        }
        session.setAttribute(SESSION_USER, user);
        return redirect("/");
    }


    @RequestMapping(value = "register", method = RequestMethod.POST)
    public String register(User user){
        User temp = userService.selectByPhone(user.getPhone());
        if(null != temp){
            throw new MessageException("该手机号已经注册");
        }
        user.setAddtime(new Date());
        user.setStatus(1);
        user.setPassword(SecurityUtil.MD5(user.getPassword(),PASSWORD_COUNT));
        userService.insertSelective(user);
        session.setAttribute(SESSION_USER, user);
        return redirect("index");
    }*/

    @RequestMapping("logout")
    public String logout(){
        session.removeAttribute(SESSION_USER);
        return redirect("index");
    }
}